<script setup>
import { getMyShelf, cancelSubscribe } from '@/api/book/index.js'
import { message } from 'ant-design-vue';
import { onMounted,ref } from 'vue'
import {  useRouter } from 'vue-router'
const books = ref()
const router = useRouter()
onMounted(() => {
  loadData()
})

const loadData = () => {
  getMyShelf({ pageNum: 1, pageSize: 20 })
    .then(res => {
      books.value = res.records
    })
}
const readNow = (id, cid) => {
  router.push(`/shuku/${id}/${cid}`)
}

const removeShelf = id => {
  cancelSubscribe({ id })
    .then(() => {
      message.success('移出成功')
      loadData()
    })
    .catch((err) => {
      message.error(err || '移出失败')
      loadData()
    })
}
</script>

<template>
<div class="book-manage">
  <div class="wrapper">
      <div class="title">书架列表</div>
      <div>书中自有颜如玉</div>
  </div>
  <div class="content">
      <div class="con" v-for="book in books" :key="book.novelId">
        <div class="wra">
          <img :src="book.cover" alt="">
          <div class="info">
            <h2>{{ book.novelTitle }}</h2>
            <div class="author">{{ book.authorName }}</div>
            <div class="cha">{{ book.chapterTitle ?  `最近阅读的章节: ${book.chapterTitle} ` : '你还未开始读书'}}</div>
            <div class="btn-wra">
              <a-button type="primary" class="btn" shape="round" @click="() => readNow(book.novelId, book.chapterId)">立即阅读</a-button>
              <a-button type="link" class="del" shape="round" @click="() => removeShelf(book.novelId)">删除</a-button>
            </div>
          </div>
        </div>
      </div>
  </div>
</div>
</template>

<style lang="less" scoped>
.wrapper {
  display:flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e7e7e7;
  padding-bottom: 20px;
  .title {
    font-size: 16px;
    font-weight: 700;
    color: #222;
    line-height: 21px;
  }
}
.content {
  margin: 0 auto;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .wra {
    padding: 20px 30px;
    &:hover {
      box-shadow: 0 0 8px 6px hsl(0deg 0% 93% / 50%);
      .del {
        display: inline-block;
        margin-left: 50px;
      }
    }
    .del {
      display: none;
    }
    display: flex;
    width: 480px;
    margin-bottom: 20px;
    .author, .cha {
      color: #999;
    }
    .author {
      margin-bottom: 4px;
    }
    img {
      width: 116px;
      height: 140px;
      margin-right: 30px;
    }
    .info {
      display: flex;
      flex-direction: column;
      .btn {
        margin-top: 20px;
        width: 120px;
      }
    }
  }
}
</style>